package com.twu.biblioteca.view.console.resolver;

import com.twu.biblioteca.enums.MapKey;
import com.twu.biblioteca.view.console.ui_element.Menu;
import com.twu.biblioteca.view.console.ui_element.MenuItem;
import com.twu.biblioteca.view.console.ViewResolver;

import java.util.List;
import java.util.Map;

public class MenuViewResolver implements ViewResolver {
  public static final String HEADER =
      "---------------------------------------\n" +
      "|                  Menu               |\n" +
      "---------------------------------------\n";

  public static final String BODY_TEMPLATE =
      "  %d. %-20s\n";

  public static final String END_LIE =
      "---------------------------------------\n"
    + " Enter a number to select menu [1 to %d]: ";

  public String getView(Map<MapKey, Object> model) {
    return HEADER + getBody(model);
  }

  private String getBody(Map<MapKey, Object> model) {
    String body = "";
    Menu menu = (Menu) model.get(MapKey.Menus);
    List<MenuItem> menuItem = menu.getMenuItems();
    for (int i = 0; i < menuItem.size(); i++) {
      body += String.format(BODY_TEMPLATE, i+1, menuItem.get(i).getTitle());
    }
    body += String.format(END_LIE, menuItem.size());
    return body;
  }
}
